package servlet;

import domain.ZhejiangPriceIndex;
import org.json.JSONArray;
import org.json.JSONObject;
import service.ZhejiangPriceIndexService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ZhejiangPriceIndexServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ZhejiangPriceIndexService zhejiangPriceIndexService = new ZhejiangPriceIndexService();
        request.setCharacterEncoding("UTF-8");
        List<ZhejiangPriceIndex> list = zhejiangPriceIndexService
                .selectByCityAndConsumeOrPrice(request.getParameter("city"));
        JSONArray jsonArray = new JSONArray();
        StringBuilder totalOfPriceSB = new StringBuilder();
        StringBuilder foodOfPriceSB = new StringBuilder();
        StringBuilder colthesOfPriceSB = new StringBuilder();
        StringBuilder totalOfConsumeSB = new StringBuilder();
        StringBuilder foodOfConsumeSB = new StringBuilder();
        StringBuilder colthesOfConsumeSB = new StringBuilder();
        StringBuilder yearSB = new StringBuilder();
        Set<String> set = new HashSet<>();
        for (ZhejiangPriceIndex zpi : list) {
            if(zpi.getConsumeOrPrice().equals("消费")){
                if (zpi.getType().equals("总体消费指数"))
                    totalOfConsumeSB.append(zpi.getValue() + ",");
                if (zpi.getType().equals("食品"))
                    foodOfConsumeSB.append(zpi.getValue() + ",");
                if (zpi.getType().equals("衣着"))
                    colthesOfConsumeSB.append(zpi.getValue() + ",");
            }
            if(zpi.getConsumeOrPrice().equals("价格")){
                if (zpi.getType().equals("总体消费指数"))
                    totalOfPriceSB.append(zpi.getValue() + ",");
                if (zpi.getType().equals("食品"))
                    foodOfPriceSB.append(zpi.getValue() + ",");
                if (zpi.getType().equals("衣着"))
                    colthesOfPriceSB.append(zpi.getValue() + ",");
            }

            if (set.add(zpi.getYear()))
                yearSB.append(zpi.getYear() + ",");
        }

        JSONObject jsonObj = new JSONObject();
        jsonObj.put("总体消费指数", totalOfConsumeSB);
        jsonObj.put("食品消费", foodOfConsumeSB);
        jsonObj.put("衣着消费", colthesOfConsumeSB);
        jsonObj.put("总体价格指数", totalOfPriceSB);
        jsonObj.put("食品价格", foodOfPriceSB);
        jsonObj.put("衣着价格", colthesOfPriceSB);
        jsonObj.put("年份", yearSB);
        jsonArray.put(jsonObj);

        response.setHeader("content-type", "text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(jsonArray.toString());
    }
}
